\documentclass{beamer}
\beamertemplateshadingbackground{red!10}{blue!10}
\usepackage{beamerthemetree}
\usepackage{pgf,pgfarrows,pgfnodes,pgfautomata,pgfheaps,pgfshade}
\usepackage{graphicx}
\usepackage{color}
\usepackage{listings}
\lstloadlanguages{perl}
\lstset{language=perl,
  tabsize=4,
  keepspaces=true,
  keywordstyle=\color{blue} \bfseries,
  commentstyle = \color{red},
  frameround=fttt,
  frame=trBL,
  numbers=left,stepnumber=1,numberstyle=\tiny,
  basicstyle=\ttfamily\small\color{black},
  stringstyle=\ttfamily,
  columns=flexible,
  breaklines=true,
  breakautoindent=true,
  showspaces=false,
}

\title{Perl by Examples}
\author{Jun Zhang}
\date{\today}

\begin{document}
\frame{\titlepage}
\section*{Outline}

\frame{
  \tiny
  \tableofcontents
}

\section[Overview]{Perl overview}

\subsection{What is Perl?}
\frame{
 
  \begin{itemize}
  \item Perl = \textbf{P}ractical \textbf{e}xtraction and \textbf{r}eport \textbf{l}anguage
  \item Perl = \textbf{P}athologically \textbf{e}clectic \textbf{r}ubbish \textbf{l}ister
  \item Meta Data about \emph{Perl}    
    \begin{itemize}
    \item \emph{Author:  } Larry Wall --- Three great virtues of programming are \textbf{laziness}, \textbf{impatience}, and \textbf{hubris}.
    \item \emph{Gender:  } Interpretive Language
    \item \emph{Birthday:} 1987
    \item \emph{Mascot:  } Camel 
    \item \emph{Born at :} Unix (but available at every computer platform presently)    
    \end{itemize}
  \end{itemize}
 
}

\subsection{Advantages and disadvantages}
\frame{
  
  \begin{itemize}
  \item Advantages
    \begin{itemize}
    \item Free available
    \item Simple but powerful syntax 
    \item Extremely strong regular expression capabilities
    \item Works particularly well in text processing 
    \item No need to consider the boring problem of memory management
    \item Object oriented (many modules available at CPAN)
    \end{itemize}
  \item Disadvantages
    \begin{itemize}
    \item The only language that looks the same before and after RSA encryption
    \item Speed! the common problem of every interpretive language
    \end{itemize}
  \end{itemize}
}

\subsection{Running Perl programs at Windows platform}
\frame{
  \begin{itemize}
  \item Install activeperl
  \item Edit Perl source files using your favorite editor (Emacs, UE, EditPlus, etc.)
  \item In console, type ``\textbf{perl filename.pl}'' to run 
  \item type ``\textbf{perl -d filename.pl}'' to debug
  \item type ``\textbf{perl -h}'' for more information
  \end{itemize}

}

\subsection{Basic syntax}
\frame{
  \begin{itemize}
  \item Perl is free form
  \item All Perl statements end in a semicolon
  \item Comments begin with \# 
  \end{itemize}
  \lstinputlisting{examples/basic_syntax.pl}
}

\section{Variables}
\frame{
  \begin{itemize}
  \item No need to declare before using
  \item Which data type you use is explicit in how you access it
  \item Three main kinds of variables
    \begin{itemize}
    \item Scalar values begin with \textbf{\$}
    \item Arrays begin with \textbf{@}
    \item Hashes begin with \textbf{\%}
    \end{itemize}
  \end{itemize}
  \lstinputlisting{examples/variables.pl}
}
\subsection{Special variables}
\frame{
  \begin{itemize}
  \item \textbf{\$\_} The default or implicit variable. 
  \item \textbf{@\_} Subroutine parameters. 
  \item \textbf{\$a},\textbf{\$b} sort comparison routine variables. 
  \item \textbf{\$digit} Regexp parenthetical capture holders. 
  \item \textbf{\$.} Current line number (or record number) of most recent filehandle. 
  \item \textbf{\$/} Input record separator. 
  \item \textbf{@ARGV} List of command line args. 
  \item \textbf{@INC} List of library paths. 
  \item \textbf{\$0} Program name. 
  \item \textbf{\ldots}
  \end{itemize}
}


\subsection{Scalar variables}
\frame{
  Scalar variables can be integers, floating numbers, strings, references and objects
  \lstinputlisting{examples/scalar_variables.pl}
}

\subsection{Strings}
\frame{
The use of connection operator and \textbf{length} function
\lstinputlisting{examples/string1.pl}
}

\frame{
The use of \textbf{index} and \textbf{substr} functions
  \lstinputlisting{examples/string2.pl}
}

\subsection{Arrays}
\frame{
The basic operations on arrays
\lstinputlisting{examples/array1.pl}
}

\frame{
The use of functions: \textbf{split},\textbf{sort} and \textbf{join}
\lstinputlisting{examples/array2.pl}
}

\frame{
How to split an array
\lstinputlisting{examples/list_slice.pl}
}

\frame{
Look the array as \textbf{stack} or \textbf{queue}
\lstinputlisting{examples/stack_queue.pl}
}

\subsection{Hashes}

\frame{
The basic operations on hash: \textbf{access},\textbf{insert},\textbf{delete}, \ldots.
\lstinputlisting{examples/hash1.pl}
}

\frame{
Iteration on hash, \textbf{sort} by \textbf{keys} or \textbf{values}
\lstinputlisting{examples/hash2.pl}
}


\frame{
The use of hash: \textbf{erase duplication}
\lstinputlisting{examples/eraseduplicate.pl}
}


\frame{
The use of hash and \textbf{grep}: \textbf{intersection of two sets}
\lstinputlisting{examples/intersection.pl}
}


\frame{
The use of hash : \textbf{term frequency counting}
\lstinputlisting{examples/tf_counting.pl}
}

\subsection{Complex data structures}

\frame{
Array of array
\lstinputlisting{examples/ref_arr.pl}
}

\frame{
An example: shuffle cards (1/2)
\lstinputlisting{examples/shuffle_cards_1.pl}
}

\frame{
An example: shuffle cards (2/2)
\lstinputlisting{examples/shuffle_cards_2.pl}
}

\section{Flow controls and subroutines}

\frame{
Flow controls
\lstinputlisting{examples/flow_control.pl}
}


\frame{
Subroutines
\lstinputlisting{examples/sub1.pl}
}

\frame{
Named parameters
\lstinputlisting{examples/named_parameters.pl}
}

\section{File and IO}
\frame{
Read from disk
\lstinputlisting{examples/read_file.pl}
}

\frame{
Write to disk
\lstinputlisting{examples/write_file.pl}
}

\frame{
Read the directory
\lstinputlisting{examples/read_dir.pl}
}
\section{Regular expressions}
\frame{
  \begin{itemize}
  \item \textbf{regular expression} or \textbf{pattern}
    \begin{itemize}
    \item A template that describes a class of strings.
    \item A formula for matching strings that follow a specified pattern.
    \item A string can either match or not match the pattern.
    \end{itemize}
  \item Things you can do with regular expressions
    \begin{itemize}
    \item Parse text
    \item Add and/or replace subsections of text
    \item Remove pieces of the text
    \end{itemize}
  \end{itemize}
  \lstinputlisting{examples/re_1.pl}
}


\subsection{Pattern matching}

\frame{
Metacharacters (1/3) 
\begin{itemize}
\item \textbf{.} Match any one character
\item \textbf{+} Match 1 or more times
\item \textbf{*} Match 0 or more times
\item \textbf{$|$} Alternation
\end{itemize}
\lstinputlisting{examples/re_2.pl}
}

\frame{
Metacharacters (2/3) 
\begin{itemize}
\item \textbf{$\backslash$} Modify the meaning of the next char
\item \textbf{$(\;\;)$} Group and capture
\item \textbf{$[\;\;]$} Define characters class
  \begin{itemize}
  \item \textbf{$\wedge$} If the first character of a class, negates that class
  \item \textbf{$-$} Unless first or last character of a class, used for a range
  \end{itemize}
\end{itemize}
\lstinputlisting{examples/re_3.pl}
}

\frame{
Metacharacters (3/3) 
\begin{itemize}
\item $\wedge$ Match at the beginning of a string or line.
\item \$ Match at the end of a string or a line.
\item $\backslash b$ Match at a `word' boundary.
\item $\backslash B$ Match at not a `word' boundary.
\end{itemize}
\lstinputlisting{examples/re_4.pl}
}

\frame{
Character class shortcuts:
\begin{itemize}
\item $\backslash d$ : $[0-9]$
\item $\backslash D$ : $[\wedge 0-9]$
\item $\backslash s$ : $[ \backslash t\backslash n\backslash r\backslash f]$
\item $\backslash S$ : $[\wedge \backslash t\backslash n\backslash r\backslash f]$  
\item $\backslash w$ : $[a-zA-Z0-9\_]$
\item $\backslash W$ : $[\wedge a-zA-Z0-9\_]$  
\end{itemize}
Other quantifiers
\begin{itemize}
\item $\{N\}$ : Match exactly N times
\item $\{N,\}$ : Match at least N times
\item $\{N,M\}$ : Match at least N but not more than M times
\end{itemize}
}

\frame{
Modifiers apply to the entire pattern

\begin{itemize}
\item /i Ignore case
\item /g Match globally
\item /s Let . match $\backslash n$
\end{itemize}
\lstinputlisting{examples/re_5.pl}
}

\subsection{Regular expression substitution}
\frame{
Modify a string variable by applying a substitution
\lstinputlisting{examples/substitution_1.pl}
}

\section{Applications}
\frame{
  Search dictionary (1/2)
  \lstinputlisting{examples/searchdic_1.pl}  
}
\frame{
  Search dictionary (2/2)
  \lstinputlisting{examples/searchdic_2.pl}  
}

\frame{
Connect to ODBC source
\lstinputlisting{examples/odbc.pl}  
}

\frame{
ASCII to unicode converter for english file
\lstinputlisting{examples/anscii2unicode.pl}  
}

\frame{
Graphic user interface
\lstinputlisting{examples/tk.pl}  
}

\frame
{
  \begin{center}
    {\huge Thanks!}
  \end{center}
}
\end{document}
